package com.learner.trade.service.impl;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.pojo.trade.Order;
import com.learner.pojo.trade.dto.CourseDto;
import com.learner.pojo.trade.dto.MemberDto;
import com.learner.trade.feign.EduCourseService;
import com.learner.trade.feign.UcenterMemberService;
import com.learner.trade.repository.OrderRepository;
import com.learner.trade.service.OrderService;
import com.learner.trade.util.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/3/10
 * @Description:
 **/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @Autowired
    private OrderRepository orderRepository;

    @Override
    public Order saveOrder(String courseId, String userId) {
        Order order = orderRepository.findByCourseIdAndMemberId(courseId, userId);
        if (order != null) {
            return order;
        }
        CourseDto courseDto = eduCourseService.getCourseDto(courseId);
        if (courseDto == null) {
            throw ExceptionUtil.createException(RestException.class, 4400001);
        }
        MemberDto member = ucenterMemberService.getMemberDtoIdByMemberId(userId);
        if (member == null) {
            throw ExceptionUtil.createException(RestException.class, 4400001);
        }
        Order orderNew = new Order();
        orderNew.setOrderNo(OrderNoUtils.getOrderNo());
        orderNew.setCourseId(courseDto.getId());
        orderNew.setCourseTitle(courseDto.getTitle());
        orderNew.setCourseCover(courseDto.getCover());
        orderNew.setTeacherName(courseDto.getTeacherName());
        orderNew.setTotalFee(courseDto.getPrice().multiply(new BigDecimal(100)));

        orderNew.setMemberId(member.getId());
        orderNew.setMobile(member.getMobile());
        orderNew.setNickname(member.getNickname());
        orderNew.setStatus(0);
        orderNew.setPayType(1);
        return orderRepository.save(orderNew);
    }

    @Override
    public Order getById(String orderId, String userId) {
        log.info("根据id查询订单信息:{}", orderId);
        return orderRepository.findByIdAndMemberId(orderId, userId);
    }

    @Override
    public boolean isBuyByCourseId(String courseId, String userId) {
        Order order = orderRepository.findByMemberIdAndCourseIdAndStatus(userId, courseId, 1);
        return order != null;
    }

    @Override
    public List<Order> selectByMemberId(String userId) {
        log.info("获取当前用户:{},的订单列表", userId);
        return orderRepository.findMemberIdList(userId);
    }

    @Override
    public void remove(String orderId, String userId) {
        orderRepository.deleteByIdAndMemberId(orderId, userId);
    }
}
